/*
* ChimneyService.java
* Created on  202015/6/15 17:08
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/15       longshihui    初始版本
*
*/

package com.infitecs.eyas.combustion.service;

import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.step.modal.StepInfo;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.krill.ajax.response.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Service
public class ChimneyService {
    @Autowired
    private ChimneyDao chimneyDao;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private CleanService cleanService;
    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private OperateFieldDao operateFieldDao;

    public CalculateChimney getCalculateChimney() {
        return chimneyDao.getCalculateChimney(sessionService.getCaseId());
    }

    public void addCalculateChimney() {
        int caseId = sessionService.getCaseId();
        chimneyDao.addCalculateChimney(caseId);
        sessionService.addStepState(new StepInfo(caseId, Constant.STEP_ID_10, Constant.STEP_OVER_STATE_0));
    }

    public void saveValueOfChimney(CalculateChimney calculateChimney) {
        calculateChimney.setCaseId(sessionService.getCaseId());
        chimneyDao.saveValueOfChimney(calculateChimney);
        calculateChimney.setParamName(sessionService.getE() + calculateChimney.getParamName());
        sessionService.putParamMap(calculateChimney);
    }

    public void chimneySave(CalculateChimney calculateChimney) {
        chimneySaveData(calculateChimney);
    }


    public void chimneySaveData(CalculateChimney calculateChimney) {
        Map<String, Double> dataMap = getChimney(calculateChimney);
        calculateChimney.setE314(dataMap.get("E314"));
        calculateChimney.setE315(dataMap.get("E315"));
        calculateChimney.setE316(dataMap.get("E316"));
        calculateChimney.setE318(dataMap.get("E318"));
        calculateChimney.setE323(dataMap.get("E323"));
        calculateChimney.setE324(dataMap.get("E324"));
        calculateChimney.setE325(dataMap.get("E325"));
        calculateChimney.setE327(dataMap.get("E327"));
        calculateChimney.setE328(dataMap.get("E328"));
        CalculateChimney oldData = chimneyDao.getCalculateChimney(calculateChimney.getCaseId());
        if (oldData == null) {
            chimneyDao.addCalculateChimneyData(calculateChimney);
        } else {
            chimneyDao.updateCalculateChimney(calculateChimney);
        }
        OperateField operateField = new OperateField();
        operateField.setE314State(calculateChimney.getE314State());
        operateField.setE315State(calculateChimney.getE315State());
        operateField.setE316State(calculateChimney.getE316State());
        operateField.setCaseId(calculateChimney.getCaseId());
        operateFieldDao.updateOperateFieldChimney(operateField);
    }

    public Map<String, Double> getChimney(CalculateChimney calculateChimney) {
        Map<String, Formula> formulaMap = expressionService.getFormulaMapByType(6);
        Map<String, Double> paramMap = cleanService.getCleanDataByCaseId(calculateChimney.getCaseId());
        paramMap.put("E306", calculateChimney.getE306());
        paramMap.put("E307", calculateChimney.getE307());
        paramMap.put("E308", calculateChimney.getE308());
        paramMap.put("E309", calculateChimney.getE309());
        if ("1".equals(calculateChimney.getE314State())) {
            paramMap.put("E314", calculateChimney.getE314());
        }
        if ("1".equals(calculateChimney.getE315State())) {
            paramMap.put("E315", calculateChimney.getE315());
        }
        if ("1".equals(calculateChimney.getE316State())) {
            paramMap.put("E316", calculateChimney.getE316());
        } else {
            //E316
            double E316 = expressionService.getCalculateData(formulaMap.get("RE316"), paramMap);
            paramMap.put("E316", E316);
        }
        //E317
        double E317 = expressionService.getCalculateData(formulaMap.get("RE317"), paramMap);
        paramMap.put("E317", E317);
        //E318
        double E318 = expressionService.getCalculateData(formulaMap.get("RE318"), paramMap);
        paramMap.put("E318", E318);
        //E319
        double E319 = expressionService.getCalculateData(formulaMap.get("RE319"), paramMap);
        paramMap.put("E319", E319);
        paramMap.put("E320", calculateChimney.getE320());
        paramMap.put("E321", calculateChimney.getE321());
        paramMap.put("E322", calculateChimney.getE322());
        //E323
        double E323 = expressionService.getCalculateData(formulaMap.get("RE323"), paramMap);
        paramMap.put("E323", E323);
        //E324
        double E324;
        switch (calculateChimney.getE320().intValue()) {
            case 0:
                E324 = expressionService.getCalculateData(formulaMap.get("RE325"), paramMap);
                break;
            case 1:
                E324 = expressionService.getCalculateData(formulaMap.get("RE324_3"), paramMap);
                break;
            case 2:
                E324 = expressionService.getCalculateData(formulaMap.get("RE324_2"), paramMap);
                break;
            case 3:
                E324 = expressionService.getCalculateData(formulaMap.get("RE324_1"), paramMap);
                break;
            default:
                E324 = -1.0;
        }
        paramMap.put("E324", E324);
        //E325
        double E325 = expressionService.getCalculateData(formulaMap.get("RE325"), paramMap);
        paramMap.put("E325", E325);
        //E326
        paramMap.put("E326", calculateChimney.getE326());
        //E327
        double E327 = expressionService.getCalculateData(formulaMap.get("RE327"), paramMap);
        paramMap.put("E327", E327);
        //E328
        double E328 = expressionService.getCalculateData(formulaMap.get("RE328"), paramMap);
        paramMap.put("E328", E328);
        //E329
        double E329 = (calculateChimney.getE326() > Math.max(E323, E324) && calculateChimney.getE326() < E325) ? 1.0 : 0.0;
        paramMap.put("E329", E329);
        //E330
        double E330 = expressionService.getCalculateData(formulaMap.get("RE330"), paramMap);
        paramMap.put("E330", E330);
        paramMap.put("E68H",   DoubleUtil.getInstance().get4Double(paramMap.get("E68")*paramMap.get("E4")/3600));
        paramMap.put("E101H",  DoubleUtil.getInstance().get4Double(paramMap.get("E101")*paramMap.get("E4")/3600));
        paramMap.put("E135H",  DoubleUtil.getInstance().get4Double(paramMap.get("E135")*paramMap.get("E4")/3600));
        paramMap.put("E168H",  DoubleUtil.getInstance().get4Double(paramMap.get("E168")*paramMap.get("E4")/3600));
        paramMap.put("E176H",  DoubleUtil.getInstance().get4Double(paramMap.get("E176")*paramMap.get("E4")/3600));
        paramMap.put("E181H",  DoubleUtil.getInstance().get4Double(paramMap.get("E181")*paramMap.get("E4")/3600));
        paramMap.put("E288H",  DoubleUtil.getInstance().get4Double(paramMap.get("E288")*paramMap.get("E4")/3600));
        paramMap.put("E170H",  DoubleUtil.getInstance().get4Double(paramMap.get("E170")+paramMap.get("E155")));
        return paramMap;
    }

    public AjaxResponse chimneyValidate(CalculateChimney calculateChimney) {
        Map<String, Double> dataMap = getChimney(calculateChimney);
        return new AjaxResponse(dataMap.get("E329") + "");
    }

    public Map<String, Double> getChimneyByCaseId(int caseId) {
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(caseId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        calculateChimney.setE314State(operateField.getE314State());
        calculateChimney.setE315State(operateField.getE315State());
        calculateChimney.setE316State(operateField.getE316State());
        calculateChimney.setCaseId(operateField.getCaseId());
        return getChimney(calculateChimney);
    }
}
